package com.yjf.ljr.application;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.AlarmManager;
import android.app.Application;
import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.PowerManager;
import android.text.TextUtils;

import com.yjf.ljr.activity.LockPatternActivity;
import com.yjf.ljr.common.CommonData;
import com.yjf.ljr.service.AutoLoginService;

public class MyApplication extends Application {

	private AlarmManager alarmManager;
	
	@Override
	public void onCreate() {
		super.onCreate();
		AppCrashHandler crashHandler = AppCrashHandler.getInstance();
		crashHandler.init(getApplicationContext());
		initDB();
		alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
	}

	private void initDB() {
	}
	
	
	/**
	 * 是否在前台运行
	 * 
	 * @param @param context
	 * @param @return
	 * @return boolean
	 * @throws
	 */
	public boolean isRunningForeground (Context context)
	{
	    ActivityManager am = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
	    ComponentName cn = am.getRunningTasks(1).get(0).topActivity;
	    String currentPackageName = cn.getPackageName();
	    if(!TextUtils.isEmpty(currentPackageName) && currentPackageName.equals(getPackageName()))
	    {
	        return true ;
	    }
	    return false ;
	}
	
	/**
	 * 开启检测界面
	 * 
	 * @param 
	 * @return void
	 * @throws
	 */
	public void verify(Activity activity) {
		boolean isTopRunning = isRunningForeground(getApplicationContext());
		if(isTopRunning){
			// 判断检测界面是否已经运行
			if(!LockPatternActivity.IS_SHOW){
				Intent intent = new Intent();
				intent.setClass(activity/*getApplicationContext()*/, LockPatternActivity.class);
//				intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//				startActivity(intent);
				activity.startActivityForResult(intent, CommonData.REQUEST_CODE_LOGIN);
			}
		}		
	}
	
	public boolean isScreenOn(Context context) {
		PowerManager pm = (PowerManager)context.getSystemService(Activity.POWER_SERVICE);
		return pm.isScreenOn();
	}
	
	/**
	 * 开启自动登陆模式
	 */
	public void startAutoLogin() {
		AutoLoginService.autoLogin = true;
		Intent intent = new Intent(this,AutoLoginService.class);
		PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
		alarmManager.cancel(pendingIntent);
        long triggerAtTime = System.currentTimeMillis()+5*1000;
        alarmManager.set(AlarmManager.RTC_WAKEUP, triggerAtTime, pendingIntent);
	}
	
	/**
	 * 操作系统为了节能省电，将会调整alarm唤醒的时间。故通过AlarmManager.setRepeating()方法将不再保证你定义的工作能按时开始,所以换种方式
	 * 而此方法供autologinservice调用已保证按时执行自动登陆那种操作
	 * 
	 * @param delay
	 */
	public void continueAutoLogin(long delay) {
		Intent intent = new Intent(this,AutoLoginService.class);
		PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
		alarmManager.cancel(pendingIntent);
        long triggerAtTime = System.currentTimeMillis()+delay;
        alarmManager.set(AlarmManager.RTC_WAKEUP, triggerAtTime, pendingIntent);
	}
	
	/**
	 * 关闭自动登陆模式
	 */
	public void stopAutoLogin() {
		AutoLoginService.autoLogin = false;
		Intent intent = new Intent(this,AutoLoginService.class);
		PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
		alarmManager.cancel(pendingIntent);
	}

}
